package yuekao6.tm3;


import com.alibaba.fastjson.JSON;
import org.apache.flink.api.common.eventtime.WatermarkStrategy;
import org.apache.flink.api.common.functions.MapFunction;
import org.apache.flink.streaming.api.datastream.DataStreamSource;
import org.apache.flink.streaming.api.datastream.SingleOutputStreamOperator;
import org.apache.flink.streaming.api.environment.StreamExecutionEnvironment;
import org.apache.flink.streaming.api.functions.windowing.ProcessAllWindowFunction;
import org.apache.flink.streaming.api.functions.windowing.ProcessWindowFunction;
import org.apache.flink.streaming.api.windowing.assigners.SlidingEventTimeWindows;
import org.apache.flink.streaming.api.windowing.assigners.TumblingEventTimeWindows;
import org.apache.flink.streaming.api.windowing.time.Time;
import org.apache.flink.streaming.api.windowing.windows.TimeWindow;
import org.apache.flink.util.Collector;
import yuekao6.entity.TransportTask;
import yuekao6.util.KafkaUtil;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.time.Duration;


public class Business {
    public static void main(String[] args) throws Exception {
        StreamExecutionEnvironment env = StreamExecutionEnvironment.getExecutionEnvironment();
        env.setParallelism(1);

        DataStreamSource<String> streamSource = env.socketTextStream("hadoop-single", 9999);

//        DataStreamSource<String> streamSource = env.addSource(KafkaUtil.kafkaSources("tms_dwd_trans_trans_finish"));
//        streamSource.print();
        SingleOutputStreamOperator<TransportTask> dataDS = streamSource.map(new MapFunction<String, TransportTask>() {
            @Override
            public TransportTask map(String s) throws Exception {
                return JSON.parseObject(s, TransportTask.class);
            }
        }).assignTimestampsAndWatermarks(WatermarkStrategy
                .<TransportTask>forBoundedOutOfOrderness(Duration.ofSeconds(0))
                .withTimestampAssigner((event, timestamp) -> {
                    try {
                        return new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss'Z'").parse(event.getCreate_time()).getTime();
                    } catch (ParseException e) {
                        throw new RuntimeException(e);
                    }
                }));

//        dataDS.print();
        //1.当日发单总数，当日转运完成运单数，当日完成运输次数，当日完成运输里程，当日完成运输时长(10分)
        SingleOutputStreamOperator<String> process = dataDS.
//                keyBy(x->"all").window(TumblingEventTimeWindows.of(Time.days(1)))
                windowAll(TumblingEventTimeWindows.of(Time.days(1)))
        .process(new ProcessAllWindowFunction<TransportTask, String, TimeWindow>() {
            @Override
            public void process(ProcessAllWindowFunction<TransportTask, String, TimeWindow>.Context context, Iterable<TransportTask> iterable, Collector<String> collector) throws Exception {
                int num = 0;
                int finish = 0;
                int count = 0;
                Double distance = 0.0;
                Long timeData = 0L;
                for (TransportTask one : iterable) {
                    num += one.getOrder_num();
                    if ("67004".equals(one.getStatus())) {
                        finish += one.getOrder_num();
                        count++;
                        distance += one.getActual_distance();
                        timeData += one.getCreate_ts();
                    }
                }
                collector.collect("当日发单总数:"+num + "---当日转运完成运单数:" + finish + "---当日完成运输次数:" + count + "---当日完成运输里程:" + distance + "---当日完成运输时长:" + timeData);
            }
        });
        process.print();

        env.execute();
    }
}
